package cn.byapps.hw.service.impl;

import cn.byapps.hw.dao.TeacherDao;
import cn.byapps.hw.entity.Clazz;
import cn.byapps.hw.entity.Course;
import cn.byapps.hw.entity.SerialPK;
import cn.byapps.hw.entity.Teacher;
import cn.byapps.hw.service.SerialPKService;
import cn.byapps.hw.service.TeacherService;
import cn.byapps.hw.utils.MD5Utils;
import cn.byapps.hw.vo.TeacherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Transactional
@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherDao teacherDao;
    @Resource
    private SerialPKService serialPKService;



    @Override
    public void add(Teacher teacher) {
        Optional<Teacher> t = teacherDao.findById(teacher.getId());
        Teacher teacher1 = t.isPresent() ? t.get() : null;
        if (teacher1 != null) {
            teacherDao.saveAndFlush(new Teacher(teacher.getId(), teacher.getName(), MD5Utils.md5(teacher.getPassword()), teacher.getPhone()));
        } else {
            teacherDao.save(new Teacher(teacher.getId(), teacher.getName(), MD5Utils.md5(teacher.getPassword()), teacher.getPhone()));
        }
    }

    @Override
    public void update(Teacher teacher) {
        Teacher t = teacherDao.getOne(teacher.getId());
        if (t != null) {
            t.setName(teacher.getName());
            t.setPassword(MD5Utils.md5(teacher.getPassword()));
            t.setPhone(teacher.getPhone());
        }
    }

    @Override
    public void deleteById(String teacherId) {
        List<SerialPK> serialPKS = serialPKService.getClazzbyTeacher(new Teacher(teacherId, null, null, null));
        for (SerialPK serialPK : serialPKS) {
            serialPK.setCourse(null);
            serialPK.setTeacher(null);
            serialPK.setClazz(null);
        }
        teacherDao.deleteById(teacherId);
    }

    @Override
    public List<TeacherVO> findVO(Integer pos, Integer rows) {
        int pageNumber = (pos + rows - 1) / rows;
        Pageable pageable = PageRequest.of(pageNumber, rows);
        Page<Teacher> page = teacherDao.findAll(pageable);
        List<Teacher> teachers = page.getContent();
        List<TeacherVO> teacherVOs = new ArrayList<TeacherVO>();
        for (Teacher teacher : teachers) {
            teacherVOs.add(new TeacherVO(teacher.getId(), teacher.getName(), teacher.getPassword(), teacher.getPhone(), serialPKService.getClazzbyTeacher(teacher).stream().map(SerialPK::getClazz).collect(Collectors.toList()), null));
        }
        return teacherVOs;
    }

    @Override
    public List<TeacherVO> findVO(Integer pos, Integer rows, String search) {
        return null;
    }

    @Override
    public List<TeacherVO> findVOByTeacher(Integer pos, Integer rows, String teacherId) {
        List<SerialPK> serialPKS = serialPKService.getClazzbyTeacher(new Teacher(teacherId, null, null, null));
        List<TeacherVO> teacherVOS = new ArrayList<>();
        for (SerialPK s : serialPKS) {
            List<Clazz> clazzs = new ArrayList<Clazz>();
            clazzs.add(s.getClazz());
            List<Course> courses = new ArrayList<Course>();
            courses.add(s.getCourse());
            teacherVOS.add(new TeacherVO(null, null, null, null, clazzs, courses));
        }
        return teacherVOS;
    }

    @Override
    public List<TeacherVO> findVO() {
        List<Teacher> teachers = teacherDao.findAll();
        List<TeacherVO> teacherVOS = new ArrayList<>();
        for (Teacher teacher : teachers) {
            teacherVOS.add(new TeacherVO(teacher.getId(), teacher.getName(), null, null, null, null));
        }
        return teacherVOS;
    }

    @Override
    public void deleteCourse(String teacherId, Integer courseId, Integer clazzId) {
        SerialPK serialPK = serialPKService.getByTeacherAndCourse(new Course(courseId, null), new Teacher(teacherId, null, null, null), new Clazz(clazzId, null, null));
        serialPKService.delete(serialPK);
    }

    @Override
    public int count() {
        return (int) teacherDao.count();
    }

    @Override
    public Teacher getByNameAndPassword(Teacher teacher) {
        Example<Teacher> teacherExample = Example.of(teacher);
        List<Teacher> teachers = teacherDao.findAll(teacherExample);
        log.info(teachers.toString());
        if (teachers.size() == 1){
            return teachers.get(0);
        }
        return null;
    }

}
